package com.rongshu.view.userInfo.activity;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.userInfo.entity.PrivacyInfo;
import com.rongshu.view.userInfo.iview.PrivacySettingView;
import com.rongshu.view.userInfo.presenter.PrivacySettingPresenter;
import com.widget.SwitchButton;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class PrivacySettingsAcitivity extends BaseActivity<PrivacySettingPresenter> implements PrivacySettingView, SwitchButton.OnCheckedChangeListener {
    @BindView(R.id.txt_top_title)
    TextView txt_top_title;//标题
    @BindView(R.id.sb_allow_recommend_mine_to_friend)
    SwitchButton sb_allow_recommend_mine_to_friend;
    @BindView(R.id.sb_allow_attention_mine)
    SwitchButton sb_allow_attention_mine;
    @BindView(R.id.sb_allow_send_message_to_mine)
    SwitchButton sb_allow_send_message_to_mine;
    @BindView(R.id.sb_allow_browse_mine_home_page)
    SwitchButton sb_allow_browse_mine_home_page;
    @BindView(R.id.sb_allow_browse_mine_pen_friend)
    SwitchButton sb_allow_browse_mine_pen_friend;
    @BindView(R.id.sb_allow_browse_join_pen_friend)
    SwitchButton sb_allow_browse_join_pen_friend;

    //map
    private Map<String, Object> map = null;
    //boolean
    private int recommend, followMe, sendOffline, browseHomePage, browseMypen, joinMypen;

    @Override
    protected int initLayout() {
        return R.layout.activity_privacy_setting_view;
    }

    @Override
    protected void initView() {
        //set presenter
        setPresenter(new PrivacySettingPresenter(this), this);
        //设置title
        txt_top_title.setText(R.string.setting_privacy);
    }

    @Override
    protected void initData() {
        //设置listener
        sb_allow_recommend_mine_to_friend.setOnCheckedChangeListener(this);
        sb_allow_attention_mine.setOnCheckedChangeListener(this);
        sb_allow_send_message_to_mine.setOnCheckedChangeListener(this);
        sb_allow_browse_mine_home_page.setOnCheckedChangeListener(this);
        sb_allow_browse_mine_pen_friend.setOnCheckedChangeListener(this);
        sb_allow_browse_join_pen_friend.setOnCheckedChangeListener(this);
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        getPrivacy();
    }

    /**
     * 获取隐私设置/消息推送设置
     */
    private void getPrivacy() {
        int user_id = UserInfoUtils.getUserId();
        map = new HashMap<>();
        map.put("user_id", user_id);
        presenter.getPrivacy(user_id, Common.encryptionSign(map));
        map.clear();
    }

    /**
     * 隐私设置
     */
    private void setPrivacy() {
        int user_id = UserInfoUtils.getUserId();
        map = new HashMap<>();
        map.put("user_id", user_id);
        map.put("recommend", recommend);
        map.put("followMe", followMe);
        map.put("sendOffline", sendOffline);
        map.put("browseHomePage", browseHomePage);
        map.put("browseMypen", browseMypen);
        map.put("joinMypen", joinMypen);
        presenter.getSetPrivacy(user_id,
                (int) map.get("recommend"),
                (int) map.get("followMe"),
                (int) map.get("sendOffline"),
                (int) map.get("browseHomePage"),
                (int) map.get("browseMypen"),
                (int) map.get("joinMypen"),
                Common.encryptionSign(map));
        map.clear();
    }

    /**
     * 更新view
     */
    private void refreshView(PrivacyInfo.ResponseBean responseBean) {
        recommend = responseBean.getAllow_recommend();
        followMe = responseBean.getAllow_me();
        sendOffline = responseBean.getAllow_send_offline();
        browseHomePage = responseBean.getAllow_browse_homepage();
        browseMypen = responseBean.getAllow_browse_mypen();
        joinMypen = responseBean.getAllow_join_mypen();
        sb_allow_recommend_mine_to_friend.setChecked(recommend == 1 ? true : false);
        sb_allow_attention_mine.setChecked(followMe == 1 ? true : false);
        sb_allow_send_message_to_mine.setChecked(sendOffline == 1 ? true : false);
        sb_allow_browse_mine_home_page.setChecked(browseHomePage == 1 ? true : false);
        sb_allow_browse_mine_pen_friend.setChecked(browseMypen == 1 ? true : false);
        sb_allow_browse_join_pen_friend.setChecked(joinMypen == 1 ? true : false);
    }

    @OnClick({R.id.layout_back, R.id.layout_blacklist})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                finish();
                break;
            case R.id.layout_blacklist://黑名单
                Intent intent = new Intent(this, BlackManagementAcitivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onCheckedChanged(SwitchButton view, boolean isChecked) {
        switch (view.getId()) {
            case R.id.sb_allow_recommend_mine_to_friend:
                recommend = isChecked ? 1 : 0;
                break;
            case R.id.sb_allow_attention_mine:
                followMe = isChecked ? 1 : 0;
                break;
            case R.id.sb_allow_send_message_to_mine:
                sendOffline = isChecked ? 1 : 0;
                break;
            case R.id.sb_allow_browse_mine_home_page:
                browseHomePage = isChecked ? 1 : 0;
                break;
            case R.id.sb_allow_browse_mine_pen_friend:
                browseMypen = isChecked ? 1 : 0;
                break;
            case R.id.sb_allow_browse_join_pen_friend:
                joinMypen = isChecked ? 1 : 0;
                break;
        }
        setPrivacy();
    }

    /**
     * 隐私设置/消息推送信息回调
     *
     * @param info
     */
    @Override
    public void onPrivacyInfo(PrivacyInfo info) {
        if (info.getErrCode() == 0) {
            refreshView(info.getResponse());
        } else {
            toast(info.getErrMsg());
        }
    }

    @Override
    public void onSetPrivacy(INetBean bean) {

    }
}
